<include file="Public:header_content" />

<div class="col-sm-12">
    <!-- Example Events -->

    <div class="example-wrap">
        <h4 class="example-title"></h4>
        <div class="example">
            <div class="btn-group hidden-xs" id="exampleTableEventsToolbar" role="group" style="float: left;margin-top: 10px;">
                <button type="button" class="btn btn-outline btn-default" id="add">
                    <i class="glyphicon glyphicon-plus" aria-hidden="true"></i>
                </button>
                </button>
                <button type="button" id="del" class="btn btn-outline btn-default">
                    <i class="glyphicon glyphicon-trash" aria-hidden="true"></i>
                </button>
            </div>
            <table id="tableList"></table>
        </div>
    </div>
    <!-- End Example Events -->
</div>

<include file="Public:footer_content" />
<script src="/Public/plugin/gb312/js/bootstrap-table-edit.js"></script>
<script src="/Public/plugin/gb312/js/plugins/bootstrap-table/bootstrap-editable.js"></script>

<script type='text/javascript'>
    var tableList = $('#tableList');
    $(function () {
        tableList.bootstrapTable({
            url: "<{:U('Member/memberLvlist')}>",
            refresh: {
                url: '<{:U("Member/memberLvlist")}>',
                silent: false,
                //pageSize:2,
                //query:{foo: 'bar'},
            },
            dataType:'json',
            method: 'post',
            contentType: "application/x-www-form-urlencoded",//post请求必带属性  application/json;charset=UTF-8
            //sortClass: 'cate_id',
            cache: false,
            showRefresh: true,
            showToggle: true,
            toolbar: '#toolbar',//工具按钮用哪个容器
            striped: true,//是否显示行间隔色
            showPaginationSwitch: true,//显示分页按钮开关
            buttonsAlign: 'right',
            //toolbarAlign: 'right',
            clickToSelect: true,
            //showHeader: false,
            //showFooter: true,
            showColumns: true,
            queryParams: queryParams,//传递参数（*）
            //cardView: true,
            //detailView: true,//是否显示父子表
            sortStable: true,
            sortOrder: 'desc',
            paginationPreText: '上一页',
            paginationNextText: '下一页',
            iconSize: 'outline',
            idField:'member_lv_id',
            pagination: true, //分页
            dataField: 'lists',//bootstrap table 可以前端分页也可以后端分页，这里我们使用的是后端分页，后端分页时需返回含有total：总记录数,这个键值好像是固定的
                             //rows： 记录集合 键值可以修改  dataField 自己定义成自己想要的就好
            maintainSelected: false,//在更改页和搜索上保持选定的行
            minimumCountColumns: 1,
            pageNumber: 1,//初始化到第几页
            queryParamsType:'limit',//查询参数组织方式
            pageSize: 18,//初始化页面条数
            pageList:[10,20,50],
            singleSelect: false,
            //data-locale:"zh-US", //表格汉化
            search: true, //显示搜索框
            searchAlign: 'left',
            searchOnEnterKey: false,
            searchTimeOut: '15',
            searchText: '',
            trimOnSearch: false,
            sidePagination: "server", //server 服务端处理分页
            uniqueId: 'member_lv_id',//每一行的唯一标识，一般为主键列
            columns: [{
                field: 'checkboxs',
                title: '全选',
                checkbox: true
            },{
                field: 'member_lv_id',
                title: 'ID',
                sortable: true,
                order:'desc',
                visible: false, //影藏该字段
            },{
                field: 'name',
                title: '等级名称',
                editable: {
                    //title: "选择部门",           //编辑框的标题
                    disabled: false,           //是否禁用编辑
                    emptytext: "0",       //空值的默认文本
                    mode: "popup",            //编辑框的模式：支持popup和inline两种模式，默认是popup
                    type: 'text',
                    validate: function (value) { //字段验证
                        if (!$.trim(value)) {
                            return '不能为空';
                        }
                    },
                    url: function(params) {
                            //alert(JSON.stringify(params));
                            var d = new $.Deferred;
                            $.ajax({
                                type: "POST",
                                url: '<{:U("Member/lvUpdate")}>',
                                data: "field=name&value="+params.value+'&id='+params.pk,
                                success: function(msg){
                                    if(msg == 'success'){
                                        swal({title:"修改成功",text:"",type:"success"});
                                        return d.resolve();
                                    }else{
                                        swal("保存失败,请稍后在试","","error");
                                        return d.reject();
                                    }
                                }
                             });
                             return d.promise();
                        }
                }
            },{
                field: 'point',
                title: '所需积分',
                sortable: true,
                 editable: {
                    //title: "选择部门",           //编辑框的标题
                    disabled: false,           //是否禁用编辑
                    emptytext: "0",       //空值的默认文本
                    mode: "popup",            //编辑框的模式：支持popup和inline两种模式，默认是popup
                    type: 'text',
                    validate: function (value) { //字段验证
                        if (!$.trim(value)) {
                            return '不能为空';
                        }
                    },
                    url: function(params) {
                            //alert(JSON.stringify(params));
                            var d = new $.Deferred;
                            $.ajax({
                                type: "POST",
                                url: '<{:U("Member/lvUpdate")}>',
                                data: "field=point&value="+params.value+'&id='+params.pk,
                                success: function(msg){
                                    if(msg == 'success'){
                                        swal({title:"修改成功",text:"",type:"success"});
                                        return d.resolve();
                                    }else{
                                        swal("保存失败,请稍后在试","","error");
                                        return d.reject();
                                    }
                                }
                             });
                             return d.promise();
                        }
                }
            },{
                field: 'tj_nums',
                title: '投票推荐数(每天)',
                sortable: true,
                 editable: {
                    //title: "选择部门",           //编辑框的标题
                    disabled: false,           //是否禁用编辑
                    emptytext: "0",       //空值的默认文本
                    mode: "popup",            //编辑框的模式：支持popup和inline两种模式，默认是popup
                    type: 'text',
                    validate: function (value) { //字段验证
                        if (!$.trim(value)) {
                            return '不能为空';
                        }
                    },
                    url: function(params) {
                            //alert(JSON.stringify(params));
                            var d = new $.Deferred;
                            $.ajax({
                                type: "POST",
                                url: '<{:U("Member/lvUpdate")}>',
                                data: "field=tj_nums&value="+params.value+'&id='+params.pk,
                                success: function(msg){
                                    if(msg == 'success'){
                                        swal({title:"修改成功",text:"",type:"success"});
                                        return d.resolve();
                                    }else{
                                        swal("保存失败,请稍后在试","","error");
                                        return d.reject();
                                    }
                                }
                             });
                             return d.promise();
                        }
                }
            },{
                field: 'shelf_nums',
                title: '书架容量',
                sortable: true,
                 editable: {
                    //title: "选择部门",           //编辑框的标题
                    disabled: false,           //是否禁用编辑
                    emptytext: "0",       //空值的默认文本
                    mode: "popup",            //编辑框的模式：支持popup和inline两种模式，默认是popup
                    type: 'text',
                    validate: function (value) { //字段验证
                        if (!$.trim(value)) {
                            return '不能为空';
                        }
                    },
                    url: function(params) {
                            //alert(JSON.stringify(params));
                            var d = new $.Deferred;
                            $.ajax({
                                type: "POST",
                                url: '<{:U("Member/lvUpdate")}>',
                                data: "field=shelf_nums&value="+params.value+'&id='+params.pk,
                                success: function(msg){
                                    if(msg == 'success'){
                                        swal({title:"修改成功",text:"",type:"success"});
                                        return d.resolve();
                                    }else{
                                        swal("保存失败,请稍后在试","","error");
                                        return d.reject();
                                    }
                                }
                             });
                             return d.promise();
                        }
                }
            },{
                field:'pre_id',
                title:'上级ID'
            },{
                field: 'default_lv',
                title: '是否默认',
                editable: {
                    //title: "选择部门",           //编辑框的标题
                    disabled: false,           //是否禁用编辑
                    emptytext: "空文本",       //空值的默认文本
                    mode: "popup",            //编辑框的模式：支持popup和inline两种模式，默认是popup
                    type: 'select',
                    source: [
                        {value:'1',text:'否'},
                        {value:'2',text:'是'}
                    ],
                    validate: function (value) { //字段验证
                        if (!$.trim(value)) {
                            return '不能为空';
                        }
                    },
                    url: function(params) {
                            //alert(JSON.stringify(params));
                            var d = new $.Deferred;
                            $.ajax({
                                type: "POST",
                                url: '<{:U("Member/lvUpdate")}>',
                                data: "field=default_lv&value="+params.value+'&id='+params.pk,
                                success: function(msg){
                                    if(msg == 'success'){
                                        swal({title:"修改成功",text:"",type:"success"});
                                        return d.resolve();
                                    }else{
                                        swal("保存失败,请稍后在试","","error");
                                        return d.reject();
                                    }
                                }
                             });
                             return d.promise();
                        }
                }
            },{
                field: 'lv_remark',
                title: '备注',
                visible: false, //影藏该字段
            },{
                field: 'disabled',
                title: '状态',
                align: 'center',
                valign: 'middle',
                editable: {
                    //title: "选择部门",           //编辑框的标题
                    disabled: false,           //是否禁用编辑
                    emptytext: "空文本",       //空值的默认文本
                    mode: "popup",            //编辑框的模式：支持popup和inline两种模式，默认是popup
                    type: 'select',
                    source: [
                        {value:'1',text:'激活'},
                        {value:'2',text:'禁用'}
                    ],
                    validate: function (value) { //字段验证
                        if (!$.trim(value)) {
                            return '不能为空';
                        }
                    },
                    url: function(params) {
                            //alert(JSON.stringify(params));
                            var d = new $.Deferred;
                            $.ajax({
                                type: "POST",
                                url: '<{:U("Member/lvUpdate")}>',
                                data: "field=disabled&value="+params.value+'&id='+params.pk,
                                success: function(msg){
                                    if(msg == 'success'){
                                        swal({title:"修改成功",text:"",type:"success"});
                                        return d.resolve();
                                    }else{
                                        swal("保存失败,请稍后在试","","error");
                                        return d.reject();
                                    }
                                }
                             });
                             return d.promise();
                        }
                }
            }],
            onClickRow: function(row,$element,field){
                $('.success').removeClass('success');
                $($element).addClass('success');
            },
            onDblClickCell: function(field, value, row, $element){
                  if(field == 'alias'){
                      //$element.empty();
                      //$element.append("<input type='text' name='alias' value='' />");
                      //return '<input type="text" name="alias" value="" />';
                  }
            }
        });



    });

    function queryParams(params){
        //alert(JSON.stringify(params));
        var temp = {   //这里的键的名字和控制器的变量名必须一直，这边改动，控制器也需要改成一样的
                limit: params.limit,   //页面大小
                offset: params.offset,  //页码
                search:params.search,
                field:params.sort,
                order: params.order//排位命令（desc，asc）
            };
        return temp;
    }

    //添加
    $('#add').click(function(){
        //iframe窗
        parent.layer.open({
            type: 2,
            title: '添加会员等级',
            //closeBtn: 1,
            //shadeClose: true,
            shade: [0.5],
            area: ['800px', '460px'],
            skin: 'layui-layer-molv',
            //offset: 'rb', //右下角弹出
            //time: 0, //2秒后自动关闭
            //shift: 2,
            content: ['<{:U("Member/addMemberLv")}>', 'no'], //iframe的url，no代表不显示滚动条
            end: function(){
                tableList.bootstrapTable('refresh',{url: '<{:U("Member/memberLvlist")}>'});
            }
        });
    });


    $('#del').click(function () {
            var ids = $.map(tableList.bootstrapTable('getSelections'), function (row) {
                return row.member_lv_id;
            });
            $.ajax({
                type: "POST",
                url: "<{:U('Member/delLv')}>",
                data: "ids="+ids,
                success: function(msg){
                  if(msg == 'success'){
                        tableList.bootstrapTable('remove', {
                            field: 'member_lv_id',
                            values: ids,
                        });
                        swal("删除成功","","success");
                  }
                }
             });

        });
</script>